package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.LogToDB
import com.alibaba.fastjson.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


/**
* @Title:回访数据信息回写客景ZO系统
* @Description:回访数据信息回写客景ZO系统--十分钟执行一次
* @Param:
* @return:
* @Author:TaoHao
* @Date: 2024/6/24
*/
public  class autoEventCallBackKJ implements  CallBackInvoke{
    private static final Log log = LogFactory.getLog(autoEventCallBackKJ.class);
    //初始化请求入参返回参数
    JSONObject outData = new JSONObject();
    JSONObject inData = new JSONObject();
    long startTime = System.currentTimeMillis();

    @Override
    void doCallBack(EliteBean eliteBean, String s, Map map, String s1) {
        try {
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            //查找待推送的话单数据--每次查询限制500条
            String selectSql='''Select Top 500 arrive_code,details_guid,arrive_time,arrive_desc  from 
                                 KJ_EVENT_LIST kel 
                                 where push_status is null  
                                 and  arrive_time>=GetDate()-1
                                  ''';

            List<Map<String, Object>> selectSqlList= jt.queryForList(selectSql);
            JSONArray ListArray=new JSONArray();//初始详情Arr
            String push_id=KHUtil.getRandomString(36);//定义一个推送ID
            if (selectSqlList.size()>0){
                for (Map<String, Object> mp2 : selectSqlList){
                    JSONObject jsonObject=new JSONObject();
                    String arrive_code=mp2.get("arrive_code");//触达状态
                    String details_guid=mp2.get("details_guid");//详情GUID
                    String arrive_time=mp2.get("arrive_time");//触达时间
                    String arrive_desc=mp2.get("arrive_desc");//触达时间
                    jsonObject.put("arrive_code",arrive_code);
                    jsonObject.put("details_guid",details_guid);
                    jsonObject.put("arrive_time",arrive_time);
                    jsonObject.put("arrive_desc",arrive_desc);


                    ListArray.add(jsonObject);
                    String updateSql='''update KJ_EVENT_LIST set push_status='1',push_time=GetDate(),push_id=?  where  details_guid=? ''';
                    jt.update(updateSql,push_id,details_guid);

                }


                inData.put("code","0");
                inData.put("msg","【autoEventCallBackKJ】处理成功！");
                inData.put("data",ListArray);
                log.debug("【autoEventCallBackKJ】准备请求ZO系统的请求报文："+inData.toString());

                //获取客景接口请求地址
                String KJ_URL = KHDBConfig.getAPIURL(eliteBean, "KJ_URL");
                //请求ZO系统接口
                Map<String, String> headMap = new HashMap<String, String>();
                headMap.put("Content-Type", "application/json");
                String PostBackBody=Util.doHttpPostUseHead(KJ_URL+"apiGetEventCallBack",inData.toString(),"UTF-8",20000000,0,headMap);



                //返回参数
                JSONObject outJSON=JSONObject.fromObject(PostBackBody);
                if (KHUtil.GetSFJV(outJSON,"code")=="0"||KHUtil.GetSFJV(outJSON,"code").equals("0")){
                    outData.put("code", "0");
                    outData.put("msg", "【apiGetEventCallBack】客景接口处理成功!耗时(毫秒)："+(System.currentTimeMillis() - startTime));

                }else{
                    outData.put("code", KHUtil.GetSFJV(outJSON,"code"));
                    outData.put("msg", "【apiGetEventCallBack】客景接口处理异常:"+outJSON.toString());

                    //处理失败,修改数据
                    String updatesql='''update KJ_EVENT_LIST set push_status is null ,push_time=GetDate() where  push_id=?'''
                    jt.update(updatesql,push_id);
                }



            }else{
                log.debug("【autoEventCallBackKJ】本次没有需要处理的详情！");
                outData.put("code", "2");
                outData.put("msg", "【autoEventCallBackKJ】接口:本次没有需要处理的详情!" );
            }






        }catch (Exception e) {
            log.error("【autoEventCallBackKJ】接口异常:" + e.getMessage());
            outData.put("code", "-1");
            outData.put("msg", "【autoEventCallBackKJ】接口异常:" +e.getMessage());
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "回访数据信息回写客景ZO系统[普通]", "autoEventCallBackKJ", inData.toString(), outData.toString(), (System.currentTimeMillis() - startTime).toString(), "/");
        }

    }
}
